/**
 * 过滤器说明
 * @FILTER_ID 过滤器ID
 * @SUB_LEAVE_OUT 过滤器方法
 * @feature 对于长字符串进行截取，从后面添加少略号
 * @auther by cattle
 * @date 2018-02-06
 */

const FILTER_ID = 'STR_SUB_LEAVE_OUT';

/**
 * @val 表达式的值
 * @num  允许字符串最长长度
 * @endNum 截取的位置
 * @sign  后面所添加的符串
 * @return {String}
 */
const SUB_LEAVE_OUT = function SUB_LEAVE_OUT(val, num, endNum, sign) {
    if (!val) {
        return '';
    }
    var len = 0;
    for (var i = 0; i < val.length; i++) {
        var a = val.charAt(i);
        if (a.match(/[^\x00-\xff]/ig) != null) {
            len += 2;
        } else {
            len += 1;
        }
    }
    let tempStr = ''
    if (len > num) {
        let vLen = val.length;
        if (endNum > vLen || endNum === vLen) {
            tempStr = val
        } else {
            tempStr = val.substring(0, endNum) + sign;
        }
    } else {
        tempStr = val;
    }
    return tempStr
}


export default {
    id: FILTER_ID,
    filterFn: SUB_LEAVE_OUT
}